/*************************************************************************
> File Name: helpaha.cpp-dfs搜索地图
> Author:ZHLH
> Mail:
> Created Time: Wed 23 Aug 2017 11:49:21 PM DST
 测试样例：
5 4
0 0 1 0
0 0 0 0
1 1 1 0
1 0 1 0
1 0 0 0
************************************************************************/

#include <iostream>

using namespace std;

struct position {
    int x, y;
}tar;
int better=99999, map[5][5], n, m, change[4][2] = { { 0,1 },{ 0,-1 },{ 1,0 },{ -1,0 } };

void dfs(int step, int x, int y)
{
    if (x == tar.x&&y == tar.y)
    {
        if (step <= better)
        {
            better = step;
        }
        return;
    }

    for (int i = 0; i < 4; i++)
    {
        int nx = x + change[i][0];
        int ny = y + change[i][1];
        if (nx >= 0 && nx < n&&ny >= 0 && ny < m && !map[nx][ny])
        {
            map[nx][ny] = 2;
            dfs(step + 1, nx, ny);
            map[nx][ny] = 0;
        }
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> map[i][j];
        }
    }
    tar.x = 3;
    tar.y = 1;
    dfs(1, 0, 0);
    cout << better;
    return 0;
}
